﻿//-----------------------------------------------------------------------
// <copyright file="StringComparison.cs" company="Rhys Parry">
//    Copyright Rhys Parry 2008
//    This file is released under the Apache 2.0 License
// </copyright>
//-----------------------------------------------------------------------
namespace Smarts.Core
{
    using System;

    /// <summary>
    /// The StringSimilarity class is used to compare two strings and derive the
    /// similarity that exists between the two
    /// </summary>
    public class StringSimilarity
    {
        /// <summary>
        /// Stores whether two strings are equal
        /// </summary>
        private readonly bool stringsEqual;

        /// <summary>
        /// Stores whether strings differ by case only
        /// </summary>
        private readonly bool differByCase;

        /// <summary>
        /// Stores whether strings differ by end whitespace only
        /// </summary>
        private readonly bool differByWhitespace;

        /// <summary>
        /// Stores whether string2 is a substring of string1
        /// </summary>
        private readonly bool string1ContainsString2;

        /// <summary>
        /// Stores whether string1 is a substring of string2
        /// </summary>
        private readonly bool string2ContainsString1;

        /// <summary>
        /// This constructor takes the two strings being compared
        /// </summary>
        /// <param name="string1">The first string</param>
        /// <param name="string2">The second string</param>
        public StringSimilarity(string string1, string string2)
        {
            if (string1 == null || string2 == null)
            {
                throw new NullReferenceException("Can not compare Null strings");
            }

            stringsEqual = string1 == string2;
            differByCase = (!Equal) && (String.Compare(string1, string2, true) == 0);
            differByWhitespace = (!Equal) && string1.Trim() == string2.Trim();
            string1ContainsString2 = string1.Contains(string2);
            string2ContainsString1 = string2.Contains(string1);
        }

        /// <summary>
        /// Gets a value indicating whether the two strings are equal
        /// </summary>
        public bool Equal
        {
            get
            {
                return stringsEqual;
            }
        }

        /// <summary>
        /// Gets a value indicating whether strings are equal in a case
        /// insensitive comparison only. Implies that the strings are not
        /// perfectly equal.
        /// </summary>
        public bool DifferByCaseOnly
        {
            get
            {
                return differByCase;
            }
        }

        /// <summary>
        /// Gets a value indicating whether the strings differ in the amount
        /// of white space on either side of the string. This does not include
        /// white space inside the string (between printable characters).
        /// Implies that the strings are not perfectly equal.
        /// </summary>
        public bool DifferByWhiteSpaceOnly
        {
            get
            {
                return differByWhitespace;
            }
        }

        /// <summary>
        /// Gets a value indicating whether String2 exists in String1
        /// </summary>
        public bool String1ContainsString2
        {
            get
            {
                return string1ContainsString2;
            }
        }

        /// <summary>
        /// Gets a value indicating whether String1 exists in String2
        /// </summary>
        public bool String2ContainsString1
        {
            get
            {
                return string2ContainsString1;
            }
        }
    }
}
